/*eslint-disable*/

/**延时等待函数，非异步式
 * @param msTime : number 延迟时间(毫秒)
 * */
function sleep_Nsync(msTime) {
    let sysDate = new Date;
    let currentTime = sysDate.getTime();
    let finalTime;
    if (Object.prototype.toString.call(msTime) === "[object Number]"
        &&
        !String(msTime).includes(".")
        &&
        !isNaN(msTime)
        &&
        isFinite(msTime)
    ) {
        finalTime = currentTime + msTime;
        while (true) {
            sysDate = new Date;
            currentTime = sysDate.getTime();
            if (currentTime >= finalTime) {
                return ("延时结束。");
            }
        }
    } else {
        throw new Error("延时时间必须是毫秒，时间值必须是数字！");
    }
}

/**延时等待函数，异步式
 * @param msTime : number 延迟时间(毫秒)
 * @return Promise
 * */
function sleep_Async(msTime) {
    return new Promise((resolve, reject) => {
        if (Object.prototype.toString.call(msTime) === "[object Number]"
            &&
            !String(msTime).includes(".")
            &&
            !isNaN(msTime)
            &&
            isFinite(msTime)
        ) {
            setTimeout(() => {
                resolve('延时结束。');
            }, msTime);
        } else {
            reject("延时时间必须是毫秒，时间值必须是正整数！");
        }
    });
}

export {sleep_Async, sleep_Nsync}